<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆名称" prop="carName">
        <el-input
          v-model="queryParams.carName"
          placeholder="请输入车辆名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成交人" prop="carName">
        <el-select v-model="queryParams.userId" placeholder="请选择" clearable filterable>
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="起拍价格" prop="startPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.startPrice"-->
<!--          placeholder="请输入起拍价格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="车辆状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable filterable>
          <el-option
            v-for="(item,index) in options"
            :key="item.index"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车源地" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入车源地"
          clearable
          @keyup.enter.native="handleQuery"
        />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gaohe-scp:car:add']"
        >新增</el-button>
      </el-col>

<!--    禁用修改按钮  -->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['gaohe-scp:car:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->

<!--      禁用删除按钮-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['gaohe-scp:car:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gaohe-scp:car:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"  :data="carList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆ID" align="center" prop="carId" />
      <el-table-column label="车辆名称" align="center" prop="carName" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="品牌" align="center" prop="brand" />-->
<!--      <el-table-column label="型号" align="center" prop="model" />-->
<!--      <el-table-column label="年限" align="center" prop="oldYear" />-->
<!--      <el-table-column label="公里数" align="center" prop="kilometers" />-->
<!--      <el-table-column label="过户次数" align="center" prop="guohuCount" />-->
<!--      <el-table-column label="评分" align="center" prop="star" />-->
      <el-table-column label="起拍价格" align="center" prop="startPrice" />
      <el-table-column label="最少加价" align="center" prop="addPrice" />
      <el-table-column label="车源地" align="center" prop="address" />
      <el-table-column label="起拍时间" align="center" prop="startTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
<!--          , '{y}-{m}-{d}'-->
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="成交人" align="center" prop="userId" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="addAuction(scope.row)"
            v-hasPermi="['gaohe-scp:car:edit']"
          >竞拍</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gaohe-scp:car:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gaohe-scp:car:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!--车辆新增表单 备用-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
    <!--        <el-form-item label="车辆名称" prop="carName">-->
    <!--          <el-input v-model="form.carName" placeholder="请输入车辆名称" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="图片1" prop="image">-->
    <!--          <image-upload v-model="form.image"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="品牌1" prop="brand">-->
    <!--          <el-input v-model="form.brand" placeholder="请输入品牌1" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="型号1" prop="model">-->
    <!--          <el-input v-model="form.model" placeholder="请输入型号1" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="年限1" prop="oldYear">-->
    <!--          <el-input v-model="form.oldYear" placeholder="请输入年限1" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="公里数1" prop="kilometers">-->
    <!--          <el-input v-model="form.kilometers" placeholder="请输入公里数1" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="过户次数1" prop="guohuCount">-->
    <!--          <el-input v-model="form.guohuCount" placeholder="请输入过户次数1" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="评分1" prop="star">-->
    <!--          <el-input v-model="form.star" placeholder="请输入评分1" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="起拍价格" prop="startPrice">-->
    <!--          <el-input v-model="form.startPrice" placeholder="请输入起拍价格" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="最少加价" prop="addPrice">-->
    <!--          <el-input v-model="form.addPrice" placeholder="请输入最少加价" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="车源地" prop="address">-->
    <!--          <el-input v-model="form.address" placeholder="请输入车源地" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="起拍时间" prop="startTime">-->
    <!--          <el-date-picker clearable-->
    <!--                          v-model="form.startTime"-->
    <!--                          type="date"-->
    <!--                          value-format="yyyy-MM-dd"-->
    <!--                          placeholder="请选择起拍时间">-->
    <!--          </el-date-picker>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="结束时间" prop="endTime">-->
    <!--          <el-date-picker clearable-->
    <!--                          v-model="form.endTime"-->
    <!--                          type="date"-->
    <!--                          value-format="yyyy-MM-dd"-->
    <!--                          placeholder="请选择结束时间">-->
    <!--          </el-date-picker>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="成交人" prop="userId">-->
    <!--          <el-input v-model="form.userId" placeholder="请输入成交人" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="备注信息" prop="remark">-->
    <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="是否删除" prop="delFlag">-->
    <!--          <el-input v-model="form.delFlag" placeholder="请输入是否删除" />-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->

    <!-- 添加或修改车辆对话框 -->

  </div>
</template>

<script>
import {addCar, delCar, getCar, listCar, updateCar} from "@/api/scp/car";
import {listUser} from "../../../api/system/user";


export default {
  name: "Car",
  data() {
    return {
      userList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carName: null,
        image: null,
        brand: null,
        model: null,
        oldYear: null,
        kilometers: null,
        guohuCount: null,
        star: null,
        startPrice: null,
        addPrice: null,
        address: null,
        startTime: null,
        endTime: null,
        status: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      active: 0,
      // 新增动态表单数据
      dynamicItems: [{ key: '', value: '' }],
      options: [{
        value: '未开始',
        label: '未开始'
      }, {
        value: '进行中',
        label: '进行中'
      }, {
        value: '已结束',
        label: '已结束'
      }, {
        value: '待提车',
        label: '待提车'
      }, {
        value: '待成交',
        label: '待成交'
      }, {
        value: '完成成交',
        label: '完成成交'
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listUser().then(response => {
        this.userList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        carName: null,
        image: null,
        brand: null,
        model: null,
        oldYear: null,
        kilometers: null,
        guohuCount: null,
        star: null,
        startPrice: null,
        addPrice: null,
        address: null,
        startTime: null,
        endTime: null,
        status: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/scp/car/form') // 保持与路由配置一致
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: '/scp/car/form',
        query: { carId: row.carId }  // 确保传递参数
      })
    },
    // 生成JSON参数
    generateParameters() {
      const params = {};
      this.dynamicItems.forEach(item => {
        if (item.key && item.value) {
          params[item.key] = item.value;
        }
      });
      return params;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$modal.confirm('是否确认删除车辆编号为"' + carIds + '"的数据项？').then(function() {
        return delCar(carIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gaohe-scp/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    },
    addAuction(row){
      // this.$router.push({path:"/scp/car/auction"})
      this.$router.push({path:"/scp/car/auction",query:{carId:row.carId}})
    }
  }
};
</script>

<style>
.dynamic-item {
  margin-bottom: 10px;
}
</style>
